﻿using MediatR;
using TaskProject.Application.Command.BOM;
using TaskProject.Domain.BOM;
using TaskProject.ErrorCode;
using TaskProject.Extension;
using TaskProject.InfrasIntructure.Interface;

namespace TaskProject.Application.Handler.BOM
{
    public class GetBOMHandler : IRequestHandler<GetBOMCommand, APIResult<List<BOMDTO>>>
    {
        IBaseRepository<BOMModel> bomRepo;
        IBaseRepository<ProductModel> proREpo;
        IBaseRepository<MaterialModel> materRepo;
        public GetBOMHandler(IBaseRepository<BOMModel> bomRepo, IBaseRepository<ProductModel> proREpo, IBaseRepository<MaterialModel> materRepo)
        {
            this.bomRepo = bomRepo;
            this.proREpo = proREpo;
            this.materRepo = materRepo;
        }

        public Task<APIResult<List<BOMDTO>>> Handle(GetBOMCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<BOMDTO>> res=new APIResult<List<BOMDTO>>();
            var boms=bomRepo.GetAll().Where(c=>!c.IsDel);
            var product = proREpo.GetAll().Where(c => !c.IsDel);
            var material = materRepo.GetAll().Where(c => !c.IsDel);
            var list = from a in boms
                       join b in product on a.ProductId equals b.ProductId
                       join c in material on a.MaterialId equals c.MaterialId
                       select new BOMDTO
                       {
                           BOMId=a.BOMId,
                           BOMtheme=a.BOMtheme,
                           BOMDesc=a.BOMDesc,
                           BOMState=a.BOMState,
                           BOMVersion=a.BOMVersion,
                           ProductId=b.ProductId,
                           ProductName=b.ProductName,
                           ProductType=b.ProductType,
                           MaterialId=c.MaterialId,
                           MaterialName=c.MaterialName,
                           Specification=c.Specification,
                           UnitName=c.UnitName,
                           Brand=c.Brand,
                           DayNum=a.DayNum,
                           PartNum=a.PartNum,
                           LossRate=a.LossRate,
                           CreateDesc=a.CreateDesc,
                           CreteDate=a.CreteDate,
                           CreteName=a.CreteName
                       };
            res.Code = APIEnum.Success;
            res.Msg = "查询BOM表成功";
            res.Data=list.ToList();
            return Task.FromResult(res);
        }
    }
}
